﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using VMoom.TemplateEngine;
using System.Text.RegularExpressions;

namespace Demo
{
	public partial class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();
		}

		private void tsmi_Convert_Click(object sender, EventArgs e)
		{
			_DoTest();
		}

		private void _DoTest()
		{
			Database db = new Database();

			string input = this.richTextBox1.Text;
			string output = "";

			Template _Public_Header = Template.FromFile("_Public_Header", Environment.CurrentDirectory + "\\Templates\\_Public_Header.html");
			Template _Public_Footer = Template.FromFile("_Public_Footer", Environment.CurrentDirectory + "\\Templates\\_Public_Footer.html");


			TemplateManager tm = TemplateManager.FromString(input);

			tm.AddTemplate(_Public_Header);
			tm.AddTemplate(_Public_Footer);

			tm.Functions.Add("RowValue", new TemplateFunction(RowValue));
			tm.Functions.Add("ColValue", new TemplateFunction(ColValue));
			tm.SetValue("Book", db.Book);
			tm.SetValue("VMoom", new string[] { "www.vmoom.com", "www.vmoom.net" });

			output = tm.Process();

			// 删除空行
			output = Regex.Replace(output, @"\n\s+\n", "\n", RegexOptions.Compiled | RegexOptions.IgnoreCase);

			//db.Book.Rows[].RowState.ToString()
			
			this.webBrowser1.DocumentText = output;

			this.richTextBox3.Text = output;
		}

		private object RowValue(object[] args)
		{
			DataRow dr = (DataRow)args[0];
			int n = (int)args[1];

			if (dr == null) { return ""; }
			if (n < 0) { n = 0; }

			return dr[n].ToString();
		}

		private object ColValue(object[] args)
		{
			DataColumn col = (DataColumn)args[0];
			int n = (int)args[1];

			if (col == null) { return ""; }
			if (n < 0) { n = 0; }

			return col.ColumnName;
		}

		private void toolStripLabel3_Click(object sender, EventArgs e)
		{
			this.webBrowser1.Refresh();
		}

		private void toolStripLabel2_Click(object sender, EventArgs e)
		{
			this.richTextBox1.Clear();
			this.richTextBox1.AppendText("{$isgt(1, 2)}<br />\r\n");
			this.richTextBox1.AppendText("{$1 lt 2}<br />\r\n");
			this.richTextBox1.AppendText("{$1 is 2}<br />\r\n");
			this.richTextBox1.AppendText("<ad:set name=\"x\" value=\"你好吗！\" />\r\n");
			this.richTextBox1.AppendText("{$x.Length}<br />\r\n");
			this.richTextBox1.AppendText("<ad:template name=\"bold\"><b>{$innerText}</b></ad:template>\r\n");
			this.richTextBox1.AppendText("<ad:template name=\"italic\"><i>{$innerText}</i></ad:template>\r\n");
			this.richTextBox1.AppendText("<ad:bold><ad:italic>测试文字</ad:italic></ad:bold><br />\r\n");

			toolStripClear();
			this.toolStripLabel2.CheckState = CheckState.Checked;
		}

		private void toolStripLabel1_Click(object sender, EventArgs e)
		{
			this.richTextBox1.Clear();
			this.richTextBox1.Text = _sl1;

			toolStripClear();
			this.toolStripLabel1.CheckState = CheckState.Checked;
		}

		private void toolStripDropDownButton1_Click(object sender, EventArgs e)
		{
			_DoTest();
		}

		private void MainForm_Load(object sender, EventArgs e)
		{
			_sl1 = this.richTextBox1.Text;
		}

		private void toolStripClear()
		{
			this.toolStripLabel1.CheckState = CheckState.Unchecked;
			this.toolStripLabel2.CheckState = CheckState.Unchecked;
		}

		private string _sl1;
	}
}